package com.example.xiaomi.Model.Service;

import com.example.xiaomi.Model.Controller.DaoList;
import com.example.xiaomi.Model.Controller.GodsList;
import com.example.xiaomi.Model.Controller.adddetailsList;
import com.example.xiaomi.Model.Controller.shoppingcartList;
import com.example.xiaomi.Model.entity.GodsEntity;
import com.example.xiaomi.Model.entity.UeserEntity;
import com.example.xiaomi.Model.entity.adddetailsEntity;
import com.example.xiaomi.Model.entity.shoppingcartEntity;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/Shopping_cart")
public class Shopping_cart extends HttpServlet {
    shoppingcartList shoppingcartList = new shoppingcartList();
    DaoList dao = new DaoList();
    GodsList godsList = new GodsList();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        UeserEntity entity = dao.selectById(Integer.valueOf(id));
        request.setAttribute("ueserName",entity.getUname());
        List<shoppingcartEntity> list = shoppingcartList.specify(Integer.parseInt(id));
        int count = 1;
        for (shoppingcartEntity entity1: list) {
            String[] arrImg = entity1.getPing().split(",");
            request.setAttribute("commodityimg"+count,arrImg[0]);
            String[] namearr = entity1.getGname().split(",");
            request.setAttribute("commodityName"+count,namearr[0]);
            request.setAttribute("commodityprice"+count,entity1.getGprice());
            request.setAttribute("commoditydamount"+count,entity1.getDamount());
            request.setAttribute("commodityId"+count,entity1.getCid());
            count++;
        }
        request.setAttribute("size",count);

        List<GodsEntity> godsEntities = godsList.selectall();
        String[][] arr = new String[godsEntities.size()][6];
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                if (j == 0){
                    arr[i][j] = godsEntities.get(i).getGname();
                }else if (j == 1){
                    arr[i][j] = String.valueOf(godsEntities.get(i).getGprice());
                }else if (j == 2){
                    arr[i][j] = godsEntities.get(i).getPing();
                }else if (j == 3){
                    arr[i][j] = godsEntities.get(i).getIs_sort();
                }else if (j == 4){
                    arr[i][j] = godsEntities.get(i).getSname();
                }else if (j == 5){
                    arr[i][j] = String.valueOf(godsEntities.get(i).getSid());
                }
            }
        }
        StringBuffer sb = new StringBuffer();
        for(int i = 0; i < arr.length; i++ ){
            for(int j = 0; j < arr[i].length; j++){
                sb.append(","+arr[i][j]);
            }
            request.setAttribute("commodity"+i,sb.toString());
            sb.setLength(0);
        }
        //长度
        request.setAttribute("length",arr.length);
        request.getRequestDispatcher("/WEB-INF/Xiaomi/Shopping_cart.jsp").forward(request,response);
    }
}
